package com.buyhouse.zhaimao.fragment;

import java.util.ArrayList;
import java.util.List;

import android.R.anim;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView.Adapter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.buyhouse.zhaimao.BaseFragment;
import com.buyhouse.zhaimao.R;
import com.buyhouse.zhaimao.adapter.NearbyExpertAdapter;
import com.buyhouse.zhaimao.model.NearByExpertBean;
import com.buyhouse.zhaimao.swipemenulistview.AbOnListViewListener;
import com.buyhouse.zhaimao.swipemenulistview.SwipeMenu;
import com.buyhouse.zhaimao.swipemenulistview.SwipeMenuCreator;
import com.buyhouse.zhaimao.swipemenulistview.SwipeMenuItem;
import com.buyhouse.zhaimao.swipemenulistview.SwipeMenuListView;
import com.buyhouse.zhaimao.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.buyhouse.zhaimao.swipemenulistview.SwipeMenuListView.OnSwipeListener;
import com.buyhouse.zhaimao.util.DensityUtil;


/**
 * 附近专家
 * 
 * @author 
 *
 */
public class NearbyExpertFragment extends BaseFragment {
	private SwipeMenuListView listview_expert;
	private NearbyExpertAdapter mAdapter;
	private Button project_expert_button,community_expert_button;
	private RelativeLayout project_expert_RelativeLayout,community_expert_RelativeLayout;
	private List<NearByExpertBean> listNearByExpertBean = new ArrayList<NearByExpertBean>();
	private List<String> listString = new ArrayList<String>();
	private TextView textView1,textView2;
	private LinearLayout linearlayout_meter;
	private ListView listview_meter;
	private ArrayAdapter mArrayAdapter;
	private TextView meter;
	@Override
	protected int getContentView() {	
		return R.layout.fragment_nearbyexpert;
	}

	@Override
	protected void initFragment() {
		
		initData();
		initView();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.project_expert_RelativeLayout:
			community_expert_button.setVisibility(View.INVISIBLE);
			project_expert_button.setVisibility(View.VISIBLE);
			textView2.setTextColor(0xffff6633);
			textView1.setTextColor(0xff000000);
			break;
		case R.id.community_expert_RelativeLayout:
			project_expert_button.setVisibility(View.INVISIBLE);
			community_expert_button.setVisibility(View.VISIBLE);
			textView1.setTextColor(0xffff6633);
			textView2.setTextColor(0xff000000);
			break;
		case R.id.linearlayout_meter:
			listview_meter.setVisibility(View.VISIBLE);
			listview_meter.setAdapter(mAdapter);
			Toast.makeText(getActivity(),"点了第xiang" , 3000).show();
			break;

		default :
			break;
		}
		
	}

	protected void initView() {
		//字体
		textView1= (TextView)rootView.findViewById(R.id.textView1);
		textView1.setTextColor(0xffff6633);
		textView2= (TextView)rootView.findViewById(R.id.textView2);
		meter= (TextView)rootView.findViewById(R.id.meter);
		//按钮
		project_expert_button = (Button)rootView.findViewById(R.id.project_expert_button);
		project_expert_button.setVisibility(View.INVISIBLE);
		community_expert_button = (Button)rootView.findViewById(R.id.community_expert_button);
		//布局
		
		linearlayout_meter = (LinearLayout)rootView.findViewById(R.id.linearlayout_meter);
		project_expert_RelativeLayout= (RelativeLayout)rootView.findViewById(R.id.project_expert_RelativeLayout);
		community_expert_RelativeLayout= (RelativeLayout)rootView.findViewById(R.id.community_expert_RelativeLayout);
		community_expert_RelativeLayout.setOnClickListener(this);
		project_expert_RelativeLayout.setOnClickListener(this);
		linearlayout_meter.setOnClickListener(this);
		
		mArrayAdapter =  new ArrayAdapter<String>(getActivity(), R.layout.item_community_sort,listString);
		listview_meter= (ListView)rootView.findViewById(R.id.listview_meter);
		listview_meter.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), 
						"你点击了第" + (position + 1) + "个项", Toast.LENGTH_LONG).show();
				listview_meter.setVisibility(View.INVISIBLE);
				meter.setText(listString.get(position).toString());
				
			}
		});
		
		
		
		mAdapter = new NearbyExpertAdapter(getActivity(), listNearByExpertBean);
		listview_expert=(SwipeMenuListView)rootView.findViewById(R.id.listview_expert);
		listview_expert.setAdapter(mAdapter);
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {

				// create "delete" item
				SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity());
				SwipeMenuItem deleteItem2 = new SwipeMenuItem(getActivity());
				// set item background
				deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
						0x3F, 0x25)));
				deleteItem2.setBackground(new ColorDrawable(Color.rgb(0x23,
						0x14, 0x56)));
				// set item width
				deleteItem.setWidth(DensityUtil.dip2px(getActivity(),60));
				deleteItem2.setWidth(DensityUtil.dip2px(getActivity(),60));
				// add to menu
				deleteItem.setIcon(R.drawable.ic_communities);
				deleteItem2.setIcon(R.drawable.button_chose);
				// add to menu
				menu.addMenuItem(deleteItem);
				menu.addMenuItem(deleteItem2);
			}
		};
		listview_expert.setMenuCreator(creator);
		
		listview_expert.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				Toast.makeText(getActivity(),"点了第"+index+"xiang" , 3000).show();
//				switch (index) {
//				case 0:
//					if (listNearByExpertBean.size() != 0) {
//						DeleteItemData(listNearByExpertBean.get(position).getId());
//						listNearByExpertBean.remove(position);
//						mAdapter.notifyDataSetChanged();
//					}
//
//					break;
//				}
			}
		});

		// set SwipeListener
		listview_expert.setOnSwipeListener(new OnSwipeListener() {

			@Override
			public void onSwipeStart(int position) {
				// swipe start
			}

			@Override
			public void onSwipeEnd(int position) {
				// swipe end
			}
		});

		// test item long click
		listview_expert.setAbOnListViewListener(new AbOnListViewListener() {

			@Override
			public void onRefresh() {
//				getData(1);
			}

			@Override
			public void onLoadMore() {
//				currentPage++;
//				getData(currentPage);
			}
		});
		listview_expert.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getActivity(), "点了list里的第"+position+"项", 2000).show();
				
			}
		});
//		isfirstLoad = false;
	}
	
	protected void initData() {
		NearByExpertBean bean = new NearByExpertBean();
		bean.setSendTime("20151010");
		NearByExpertBean bean2 = new NearByExpertBean();
		bean2.setSendTime("20151012");
		listNearByExpertBean.add(bean);
		listNearByExpertBean.add(bean2);
		listString.add("100  米");
		listString.add("200  米");
		listString.add("300  米");
	}
}
